<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定价计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.5;
            max-width: 800px;
            margin: 0 auto;
            padding: 10px;
        }

        .form-container {
            background-color: #f9f9f9;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        .form-row {
            display: flex;
            flex-wrap: wrap;
            margin-bottom: 8px;
        }

        .form-group {
            flex: 1;
            min-width: 120px;
            margin-right: 8px;
            margin-bottom: 8px;
        }

        label {
            display: block;
            margin-bottom: 3px;
            font-size: 14px;
            color: #333;
        }

        input,
        select {
            width: 100%;
            padding: 6px;
            font-size: 14px;
            border: 1px solid #ddd;
            border-radius: 3px;
            box-sizing: border-box;
        }

        .form-section {
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px solid #eee;
        }

        .form-section h3 {
            margin-top: 0;
            font-size: 16px;
            color: #555;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 8px 12px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            font-size: 14px;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 15px;
            padding: 10px;
            background-color: #e8f5e9;
            border-radius: 3px;
            display: none;
            font-size: 14px;
        }

        .tabs {
            display: flex;
            margin-bottom: 10px;
        }

        .tab {
            padding: 8px 15px;
            cursor: pointer;
            background-color: #eee;
            margin-right: 5px;
            border-radius: 3px 3px 0 0;
            font-size: 14px;
        }

        .tab.active {
            background-color: #f9f9f9;
            font-weight: bold;
        }

        .tab-content {
            display: none;
        }

        .tab-content.active {
            display: block;
        }
    </style>
</head>

<body>
    <h1>定价计算器</h1>

    <div class="tabs">
        <div class="tab active" onclick="switchTab('calculateY')">计算税前售价</div>
        <div class="tab" onclick="switchTab('calculateOff')">计算折扣率</div>
    </div>

    <div id="calculateY" class="tab-content active">
        <div class="form-container">
            <form id="yForm">
                <div class="form-row">
                    <div class="form-group">
                        <label for="profitValue">利润值(%):</label>
                        <input type="number" id="profitValue" step="0.01" value="15" required>
                    </div>

                    <div class="form-group">
                        <label for="profitType">利润类型:</label>
                        <select id="profitType" required>
                            <option value="fixed">固定利润</option>
                            <option value="cost">成本利润率</option>
                            <option value="price" selected>售价利润率</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="cost">成本（采购价 + 货代）:</label>
                        <input type="number" id="cost" step="0.01" required>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="_coupon">优惠券(外币):</label>
                        <input type="number" id="_coupon" step="1" value="2">
                    </div>

                    <div class="form-group">
                        <label for="off">折扣:</label>
                        <input type="number" id="off" step="1" min="1" max="9" value="3" required>
                    </div>

                    <div class="form-group">
                        <label for="weight">重量(g):</label>
                        <input type="number" id="weight" step="1" required>
                    </div>
                </div>

                <div class="form-section">
                    <h3>高级参数</h3>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="withdrawalFeeRate">提现手续费率:</label>
                            <input type="number" id="withdrawalFeeRate" step="0.001" value="0.01">
                        </div>

                        <div class="form-group">
                            <label for="platformCommissionRate">平台佣金比例:</label>
                            <input type="number" id="platformCommissionRate" step="0.001" value="0.054">
                        </div>

                        <div class="form-group">
                            <label for="sfpFeeRate">SFP服务费率:</label>
                            <input type="number" id="sfpFeeRate" step="0.001" value="0">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="talentCommissionRate">达人佣金比例:</label>
                            <input type="number" id="talentCommissionRate" step="0.001" value="0">
                        </div>

                        <div class="form-group">
                            <label for="transactionFeeRate">交易手续费率:</label>
                            <input type="number" id="transactionFeeRate" step="0.001" value="0.0378">
                        </div>

                        <div class="form-group">
                            <label for="vatRate">增值税率:</label>
                            <input type="number" id="vatRate" step="0.01" value="0.1">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="logisticsCostPer10g">跨境物流成本(每10g):</label>
                            <input type="number" id="logisticsCostPer10g" step="0.01" value="0.15">
                        </div>

                        <div class="form-group">
                            <label for="exchangeRate">汇率(外币→本币):</label>
                            <input type="number" id="exchangeRate" step="0.01" value="1.6412">
                        </div>

                        <div class="form-group">
                            <label for="buyerShippingFee">买家支付运费(外币):</label>
                            <input type="number" id="buyerShippingFee" step="0.01" value="10">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="bonusCashbackRate">奖金返现服务费率:</label>
                            <input type="number" id="bonusCashbackRate" step="0.01" value="0.0435">
                        </div>
                        <div class="form-group">
                        </div>
                        <div class="form-group">
                        </div>
                    </div>
                </div>

                <button type="button" onclick="calculateY()">计算税前售价</button>
            </form>

            <div id="yResult" class="result">
                <!-- <h3>计算结果:</h3>
                <p>税前售价: <span id="yValue"></span></p> -->
            </div>
        </div>
    </div>

    <div id="calculateOff" class="tab-content">
        <div class="form-container">
            <form id="offForm">
                <div class="form-row">
                    <div class="form-group">
                        <label for="off_y">税前售价(CNY):</label>
                        <input type="number" id="off_y" step="0.01" required>
                    </div>

                    <div class="form-group">
                        <label for="off_profitValue">利润值(%):</label>
                        <input type="number" id="off_profitValue" step="0.01" value="15" required>
                    </div>

                    <div class="form-group">
                        <label for="off_profitType">利润类型:</label>
                        <select id="off_profitType" required>
                            <option value="fixed">固定利润</option>
                            <option value="cost">成本利润率</option>
                            <option value="price" selected>售价利润率</option>
                        </select>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="off_cost">成本（采购价 + 货代）:</label>
                        <input type="number" id="off_cost" step="0.01" required>
                    </div>

                    <div class="form-group">
                        <label for="off__coupon">优惠券(外币):</label>
                        <input type="number" id="off__coupon" step="1" value="2">
                    </div>

                    <div class="form-group">
                        <label for="off_weight">重量(g):</label>
                        <input type="number" id="off_weight" step="1" required>
                    </div>
                </div>

                <div class="form-section">
                    <h3>高级参数</h3>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="off_withdrawalFeeRate">提现手续费率:</label>
                            <input type="number" id="off_withdrawalFeeRate" step="0.001" value="0.01">
                        </div>

                        <div class="form-group">
                            <label for="off_platformCommissionRate">平台佣金比例:</label>
                            <input type="number" id="off_platformCommissionRate" step="0.001" value="0.054">
                        </div>

                        <div class="form-group">
                            <label for="off_sfpFeeRate">SFP服务费率:</label>
                            <input type="number" id="off_sfpFeeRate" step="0.001" value="0">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="off_talentCommissionRate">达人佣金比例:</label>
                            <input type="number" id="off_talentCommissionRate" step="0.001" value="0">
                        </div>

                        <div class="form-group">
                            <label for="off_transactionFeeRate">交易手续费率:</label>
                            <input type="number" id="off_transactionFeeRate" step="0.001" value="0.0378">
                        </div>

                        <div class="form-group">
                            <label for="off_vatRate">增值税率:</label>
                            <input type="number" id="off_vatRate" step="0.01" value="0.1">
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="off_logisticsCostPer10g">跨境物流成本(每10g):</label>
                            <input type="number" id="off_logisticsCostPer10g" step="0.01" value="0.15">
                        </div>

                        <div class="form-group">
                            <label for="off_exchangeRate">汇率(外币→本币):</label>
                            <input type="number" id="off_exchangeRate" step="0.01" value="1.6412">
                        </div>

                        <div class="form-group">
                            <label for="off_buyerShippingFee">买家支付运费(外币):</label>
                            <input type="number" id="off_buyerShippingFee" step="0.01" value="10">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label for="off_bonusCashbackRate">奖金返现服务费率:</label>
                            <input type="number" id="off_bonusCashbackRate" step="0.01" value="0.0435">
                        </div>
                        <div class="form-group">
                        </div>
                        <div class="form-group">
                        </div>
                    </div>
                </div>

                <button type="button" onclick="calculateOff()">计算折扣率</button>
            </form>

            <div id="offResult" class="result">
                <!-- <h3>计算结果:</h3>
                <p>Off: <span id="offValue"></span></p> -->
            </div>
        </div>
    </div>

    <script>
        // 更新利润值标签的单位
        document.getElementById('profitType').addEventListener('change', function () {
            const profitValueLabel = document.querySelector('label[for="profitValue"]');
            if (this.value === 'fixed') {
                profitValueLabel.textContent = '利润值(CNY):';
            } else {
                profitValueLabel.textContent = '利润值(%):';
            }
        });

        document.getElementById('off_profitType').addEventListener('change', function () {
            const profitValueLabel = document.querySelector('label[for="off_profitValue"]');
            if (this.value === 'fixed') {
                profitValueLabel.textContent = '利润值(CNY):';
            } else {
                profitValueLabel.textContent = '利润值(%):';
            }
        });

        // 税前售价 1.1y = ((profit + cost) /（1 - 提现手续费）+ 平台佣金 + SFP服务费 + 达人佣金 + 交易手续费 + 增值税 + 商品实际的运费 + 奖金返现服务费 + coupon) / off - 跨境物流成本 * 1.1
        // 提现手续费 = 0.01
        // 平台佣金 = (local * off - coupon) * 0.054
        // SFP服务费 = (local * off - coupon) * 0
        // 达人佣金 = (local * off - coupon)  * 0
        // 交易手续费 = pay * 0.0378
        // 增值税 = (local * off - coupon) / 1.1 * 0.1
        // 商品实际的运费 = deliver
        // 跨境物流成本 deliver = Math.ceil(weight / 10) * 0.15 * 1.6412
        // 本地展示价 local = (y + deliver) * 1.1
        // 买家实付 pay = local * off - coupon + 16.4
        // 奖金返现服务费 = pay * 0.0435
        // 自定义进位函数（直接向上进位）

        function toFixedCeil(num, decimals) {
            const factor = Math.pow(10, decimals);
            return Math.ceil(num * factor) / factor;
        }

        function switchTab(tabId) {
            // 隐藏所有标签内容
            document.querySelectorAll('.tab-content').forEach(content => {
                content.classList.remove('active');
            });

            // 取消所有标签的激活状态
            document.querySelectorAll('.tab').forEach(tab => {
                tab.classList.remove('active');
            });

            // 激活选中的标签和内容
            document.getElementById(tabId).classList.add('active');
            event.currentTarget.classList.add('active');
        }

        function calculateY() {
            // 获取所有输入值
            const profitValue = parseFloat(document.getElementById('profitValue').value);
            const profitType = document.getElementById('profitType').value;
            const cost = parseFloat(document.getElementById('cost').value);
            const _coupon = parseFloat(document.getElementById('_coupon').value) || 0;
            const off = parseFloat(document.getElementById('off').value) / 10;
            const weight = parseFloat(document.getElementById('weight').value);
            const withdrawalFeeRate = parseFloat(document.getElementById('withdrawalFeeRate').value) || 0;
            const platformCommissionRate = parseFloat(document.getElementById('platformCommissionRate').value) || 0;
            const sfpFeeRate = parseFloat(document.getElementById('sfpFeeRate').value) || 0;
            const talentCommissionRate = parseFloat(document.getElementById('talentCommissionRate').value) || 0;
            const transactionFeeRate = parseFloat(document.getElementById('transactionFeeRate').value) || 0;
            const vatRate = parseFloat(document.getElementById('vatRate').value) || 0;
            const bonusCashbackRate = parseFloat(document.getElementById('bonusCashbackRate').value) || 0;
            const logisticsCostPer10g = parseFloat(document.getElementById('logisticsCostPer10g').value) || 0;
            const exchangeRate = parseFloat(document.getElementById('exchangeRate').value) || 1;
            const buyerShippingFee = parseFloat(document.getElementById('buyerShippingFee').value) || 0;

            // 计算税前售价(Y)
            const result = _calculateY(
                profitValue,
                profitType,
                cost,
                _coupon,
                off,
                weight,
                withdrawalFeeRate,
                platformCommissionRate,
                sfpFeeRate,
                talentCommissionRate,
                transactionFeeRate,
                vatRate,
                bonusCashbackRate,
                logisticsCostPer10g,
                exchangeRate,
                buyerShippingFee
            );

            // 显示详细计算结果
            let detailsHTML = `
                <h3>计算结果:</h3>
                <p>税前售价: <span>${toFixedCeil(result.y, 2)} (CNY)  ${toFixedCeil(result.y / exchangeRate, 2)} (外币)</span></p>
                <p>本地展示价: ${result.local.toFixed(2)} (CNY)  ${toFixedCeil(result.local / exchangeRate, 2)} (外币)</p>
                <h4>详细计算:</h4>
                <p>预估买家实付: ${(result.pay / exchangeRate).toFixed(2)} (外币)</p>
                <p>跨境物流成本: ${result.crossBorderDelivery.toFixed(2)} (CNY)</p>
                <p>平台佣金: ${result.platformCommission.toFixed(2)} (CNY)</p>
                <p>SFP服务费: ${result.sfpFee.toFixed(2)} (CNY)</p>
                <p>达人佣金: ${result.talentCommission.toFixed(2)} (CNY)</p>
                <p>交易手续费: ${result.transactionFee.toFixed(2)} (CNY)</p>
                <p>增值税: ${result.vat.toFixed(2)} (CNY)</p>
                <p>奖金返现服务费: ${result.bonusCashbackFee.toFixed(2)} (CNY)</p>
                <p>预估利润: ${result.actualProfit.toFixed(2)} (CNY)</p>
            `;

            document.getElementById('yResult').innerHTML = detailsHTML;
            document.getElementById('yResult').style.display = 'block';
        }

        function calculateOff() {
            // 获取所有输入值
            const y = parseFloat(document.getElementById('off_y').value);
            const profitValue = parseFloat(document.getElementById('off_profitValue').value);
            const profitType = document.getElementById('off_profitType').value;
            const cost = parseFloat(document.getElementById('off_cost').value);
            const _coupon = parseFloat(document.getElementById('off__coupon').value) || 0;
            const weight = parseFloat(document.getElementById('off_weight').value);
            const withdrawalFeeRate = parseFloat(document.getElementById('off_withdrawalFeeRate').value) || 0;
            const platformCommissionRate = parseFloat(document.getElementById('off_platformCommissionRate').value) || 0;
            const sfpFeeRate = parseFloat(document.getElementById('off_sfpFeeRate').value) || 0;
            const talentCommissionRate = parseFloat(document.getElementById('off_talentCommissionRate').value) || 0;
            const transactionFeeRate = parseFloat(document.getElementById('off_transactionFeeRate').value) || 0;
            const vatRate = parseFloat(document.getElementById('off_vatRate').value) || 0;
            const bonusCashbackRate = parseFloat(document.getElementById('off_bonusCashbackRate').value) || 0;
            const logisticsCostPer10g = parseFloat(document.getElementById('off_logisticsCostPer10g').value) || 0;
            const exchangeRate = parseFloat(document.getElementById('off_exchangeRate').value) || 1;
            const buyerShippingFee = parseFloat(document.getElementById('off_buyerShippingFee').value) || 0;

            // 计算折扣率(Off)
            const result = _calculateOff(
                y,
                profitValue,
                profitType,
                cost,
                _coupon,
                weight,
                withdrawalFeeRate,
                platformCommissionRate,
                sfpFeeRate,
                talentCommissionRate,
                transactionFeeRate,
                vatRate,
                bonusCashbackRate,
                logisticsCostPer10g,
                exchangeRate,
                buyerShippingFee
            );

            // 显示详细计算结果
            let detailsHTML = `
                <h3>计算结果:</h3>
                <p>折扣率: <span>${((1 - result.off) * 100).toFixed(2)}%</span></p>
                <h4>详细计算:</h4>
                <p>本地展示价: ${result.local.toFixed(2)} (CNY)  ${toFixedCeil(result.local / exchangeRate, 2)} (外币)</p>
                <p>跨境物流成本: ${result.crossBorderDelivery.toFixed(2)} (CNY)</p>
                <p>预估买家实付: ${(result.pay / exchangeRate).toFixed(2)} (外币)</p>
                <p>平台佣金: ${result.platformCommission.toFixed(2)} (CNY)</p>
                <p>SFP服务费: ${result.sfpFee.toFixed(2)} (CNY)</p>
                <p>达人佣金: ${result.talentCommission.toFixed(2)} (CNY)</p>
                <p>交易手续费: ${result.transactionFee.toFixed(2)} (CNY)</p>
                <p>增值税: ${result.vat.toFixed(2)} (CNY)</p>
                <p>奖金返现服务费: ${result.bonusCashbackFee.toFixed(2)} (CNY)</p>
                <p>预估利润: ${result.actualProfit.toFixed(2)} (CNY)</p>
            `;

            document.getElementById('offResult').innerHTML = detailsHTML;
            document.getElementById('offResult').style.display = 'block';
        }


        function _calculateY(
            // 利润值（根据profitType不同而含义不同）
            profitValue,
            // 利润类型：'fixed'（固定利润），'cost'（成本利润率），'price'（售价利润率）
            profitType = 'fixed',
            // 采购价
            cost,
            // 优惠券
            _coupon,
            // 折扣
            off,
            // 重量g
            weight,
            // 提现手续费率（1%）
            withdrawalFeeRate = 0.01,
            // 平台佣金比例（5.4%）
            platformCommissionRate = 0.054,
            // SFP服务费率（当前为0）
            sfpFeeRate = 0,
            // 达人佣金比例（当前为1%）
            talentCommissionRate = 0.01,
            // 交易手续费率（3.78%）
            transactionFeeRate = 0.0378,
            // 增值税率（10%）
            vatRate = 0.1,
            // 奖金返现服务费
            bonusCashbackRate = 0.0435,
            // 跨境物流成本：每10克的价格（0.15指定币种）
            logisticsCostPer10g = 0.15,
            // 汇率
            exchangeRate = 1.6412,
            // 买家支付的运费
            buyerShippingFee = 10
        ) {
            // 计算跨境物流成本
            const crossBorderDelivery = Math.ceil(weight / 10) * logisticsCostPer10g * exchangeRate;

            // 计算固定费用（买家支付的运费 * 汇率）
            const FIXED_FEE = buyerShippingFee * exchangeRate;
            const LOCAL_MARKUP = 1 + vatRate;
            const coupon = exchangeRate * _coupon;

            // 定义函数来计算本地展示价和买家实付
            function calculateLocalAndPay(y) {
                const deliver = crossBorderDelivery;
                const local = (y + deliver) * LOCAL_MARKUP;
                const pay = local * off - coupon + FIXED_FEE;
                return { local, pay };
            }

            // 定义目标函数，用于牛顿迭代法
            function targetFunction(y) {
                const { local, pay } = calculateLocalAndPay(y);

                // 根据利润类型计算实际利润
                let actualProfit;
                switch (profitType) {
                    case 'fixed':
                        actualProfit = profitValue;
                        break;
                    case 'cost':
                        actualProfit = cost * (profitValue / 100); // profitValue是百分比
                        break;
                    case 'price':
                        const sellingPrice = local * off - coupon;
                        actualProfit = sellingPrice * (profitValue / 100); // profitValue是百分比
                        break;
                    default:
                        throw new Error('未知的利润类型: ' + profitType);
                }

                // 平台佣金
                const platformCommission = (local * off - coupon) * platformCommissionRate;
                // sfp服务费
                const sfpFee = (local * off - coupon) * sfpFeeRate;
                // 达人佣金
                const talentCommission = (local * off - coupon) * talentCommissionRate;
                // 交易手续费
                const transactionFee = pay * transactionFeeRate;
                // 奖金返现服务费
                const bonusCashbackFee = pay * bonusCashbackRate;
                // 增值税
                const vat = (pay - FIXED_FEE) / LOCAL_MARKUP * vatRate;
                // 实际运费
                const actualDelivery = crossBorderDelivery;

                const numerator = (actualProfit + cost) / (1 - withdrawalFeeRate) +
                    platformCommission +
                    sfpFee +
                    talentCommission +
                    transactionFee +
                    vat +
                    actualDelivery +
                    bonusCashbackFee +
                    coupon;

                const denominator = off;

                return LOCAL_MARKUP * y - (numerator / denominator - crossBorderDelivery * LOCAL_MARKUP);
            }

            // 使用牛顿迭代法求解y
            let y = 10; // 初始猜测值
            const tolerance = 0.0001; // 容差
            const maxIterations = 100; // 最大迭代次数
            let iteration = 0;
            let error = 1;

            while (iteration < maxIterations && error > tolerance) {
                const f = targetFunction(y);

                // 计算导数（数值近似）
                const h = 0.0001;
                const fPlusH = targetFunction(y + h);
                const derivative = (fPlusH - f) / h;

                // 避免除以零
                if (Math.abs(derivative) < 1e-10) {
                    break;
                }

                const yNew = y - f / derivative;
                error = Math.abs(yNew - y);
                y = yNew;
                iteration++;
            }

            // 计算详细结果
            const { local, pay } = calculateLocalAndPay(y);
            // 根据利润类型计算实际利润
            let actualProfit;
            switch (profitType) {
                case 'fixed':
                    actualProfit = profitValue;
                    break;
                case 'cost':
                    actualProfit = cost * (profitValue / 100);
                    break;
                case 'price':
                    const sellingPrice = local * off - coupon;
                    actualProfit = sellingPrice * (profitValue / 100);
                    break;
            }

            // 计算各项费用
            const platformCommission = (local * off - coupon) * platformCommissionRate;
            const sfpFee = (local * off - coupon) * sfpFeeRate;
            const talentCommission = (local * off - coupon) * talentCommissionRate;
            const transactionFee = pay * transactionFeeRate;
            const bonusCashbackFee = pay * bonusCashbackRate;
            const vat = (pay - FIXED_FEE) / LOCAL_MARKUP * vatRate;

            return {
                y,
                crossBorderDelivery,
                local,
                pay,
                platformCommission,
                sfpFee,
                talentCommission,
                transactionFee,
                vat,
                bonusCashbackFee,
                actualProfit
            };
        }

        function _calculateOff(
            // 税前售价
            y,
            // 利润值（根据profitType不同而含义不同）
            profitValue,
            // 利润类型：'fixed'（固定利润），'cost'（成本利润率），'price'（售价利润率）
            profitType = 'fixed',
            // 采购价
            cost,
            // 优惠券
            _coupon,
            // 重量g
            weight,
            // 提现手续费率（1%）
            withdrawalFeeRate = 0.01,
            // 平台佣金比例（5.4%）
            platformCommissionRate = 0.054,
            // SFP服务费率（当前为0）
            sfpFeeRate = 0,
            // 达人佣金比例（当前为1%）
            talentCommissionRate = 0.01,
            // 交易手续费率（3.78%）
            transactionFeeRate = 0.0378,
            // 增值税率（10%）
            vatRate = 0.1,
            // 奖金返现服务费
            bonusCashbackRate = 0.0435,
            // 跨境物流成本：每10克的价格（0.15指定币种）
            logisticsCostPer10g = 0.15,
            // 汇率
            exchangeRate = 1.6412,
            // 买家支付的运费
            buyerShippingFee = 10
        ) {
            // 计算跨境物流成本
            const crossBorderDelivery = Math.ceil(weight / 10) * logisticsCostPer10g * exchangeRate;

            // 计算固定费用（买家支付的运费 * 汇率）
            const FIXED_FEE = buyerShippingFee * exchangeRate;
            const LOCAL_MARKUP = 1 + vatRate;
            const coupon = exchangeRate * _coupon;

            // 定义目标函数，用于牛顿迭代法
            function targetFunction(off) {
                // 计算本地展示价和买家实付
                const deliver = crossBorderDelivery;
                const local = (y + deliver) * LOCAL_MARKUP;
                const sellingPrice = local * off - coupon; // 折后售价（扣除优惠券）
                const pay = sellingPrice + FIXED_FEE;

                // 根据利润类型计算实际利润
                let actualProfit;
                switch (profitType) {
                    case 'fixed':
                        actualProfit = profitValue;
                        break;
                    case 'cost':
                        actualProfit = cost * (profitValue / 100); // profitValue是百分比
                        break;
                    case 'price':
                        actualProfit = sellingPrice * (profitValue / 100); // profitValue是百分比
                        break;
                    default:
                        throw new Error('未知的利润类型: ' + profitType);
                }

                // 平台佣金
                const platformCommission = sellingPrice * platformCommissionRate;
                // sfp服务费
                const sfpFee = sellingPrice * sfpFeeRate;
                // 达人佣金
                const talentCommission = sellingPrice * talentCommissionRate;
                // 交易手续费
                const transactionFee = pay * transactionFeeRate;
                // 奖金返现服务费
                const bonusCashbackFee = pay * bonusCashbackRate;
                // 增值税
                const vat = (pay - FIXED_FEE) / LOCAL_MARKUP * vatRate;
                // 实际运费
                const actualDelivery = crossBorderDelivery;

                // 计算方程两边
                const leftSide = LOCAL_MARKUP * y;
                const numerator = (actualProfit + cost) / (1 - withdrawalFeeRate) +
                    platformCommission +
                    sfpFee +
                    talentCommission +
                    transactionFee +
                    vat +
                    actualDelivery +
                    bonusCashbackFee +
                    coupon;
                const rightSide = numerator / off - crossBorderDelivery * LOCAL_MARKUP;

                return leftSide - rightSide;
            }

            // 使用牛顿迭代法求解off
            let off = 0.5; // 初始猜测值（50%折扣）
            const tolerance = 0.0001; // 容差
            const maxIterations = 100; // 最大迭代次数
            let iteration = 0;
            let error = 1;

            while (iteration < maxIterations && error > tolerance) {
                const f = targetFunction(off);

                // 计算导数（数值近似）
                const h = 0.0001;
                const fPlusH = targetFunction(off + h);
                const derivative = (fPlusH - f) / h;

                // 避免除以零
                if (Math.abs(derivative) < 1e-10) {
                    break;
                }

                let offNew = off - f / derivative;
                // 确保折扣率在合理范围内（0.01到1之间）
                offNew = Math.max(0.01, Math.min(1, offNew));

                error = Math.abs(offNew - off);
                off = offNew;
                iteration++;
            }

            // 计算详细结果
            const deliver = crossBorderDelivery;
            const local = (y + deliver) * LOCAL_MARKUP;
            const sellingPrice = local * off - coupon;
            const pay = sellingPrice + FIXED_FEE;

            // 根据利润类型计算实际利润
            let actualProfit;
            switch (profitType) {
                case 'fixed':
                    actualProfit = profitValue;
                    break;
                case 'cost':
                    actualProfit = cost * (profitValue / 100);
                    break;
                case 'price':
                    actualProfit = sellingPrice * (profitValue / 100);
                    break;
            }

            // 计算各项费用
            const platformCommission = sellingPrice * platformCommissionRate;
            const sfpFee = sellingPrice * sfpFeeRate;
            const talentCommission = sellingPrice * talentCommissionRate;
            const transactionFee = pay * transactionFeeRate;
            const bonusCashbackFee = pay * bonusCashbackRate;
            const vat = (pay - FIXED_FEE) / LOCAL_MARKUP * vatRate;

            return {
                off,
                crossBorderDelivery,
                local,
                pay,
                platformCommission,
                sfpFee,
                talentCommission,
                transactionFee,
                vat,
                bonusCashbackFee,
                actualProfit
            };

        }
    </script>
</body>

</html>